package com.jinbo.footmessage.serverInf;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jinbo.footmessage.BaseActivity;
import com.jinbo.footmessage.MyApplication;
import com.jinbo.footmessage.R;
import com.jinbo.footmessage.httpbean.ProjectBean;
import com.jinbo.footmessage.httpbean.WorkerInformationBean;
import com.jinbo.footmessage.httpretrofit.HttpProtocolFactory;
import com.jinbo.footmessage.utils.LocalPreferencesHelper;
import com.jinbo.footmessage.view.TitleBarView;

import de.hdodenhof.circleimageview.CircleImageView;


public class ServerInfActivity extends BaseActivity
        implements View.OnClickListener{//},ProjectItemListViewAdapter.Callbacks {
   // private Callbacks mCallBacks;
  //  private Callbacks2 mCallBacks2;
    private ImageView mLeftLineIv, mRightLineIv,mShopIv;
    private Button mLeftBt, mRightBt,mOrderBt;
    private ViewPager vp;
    private TextView mNumTv;
    private static String SERVER_INTENT="server_bean";

    public static void launch(Activity activity,WorkerInformationBean serverBean) {
        Intent intent = new Intent(activity, ServerInfActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(SERVER_INTENT, serverBean);
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }

    TitleBarView titleBarView;
    CircleImageView head;
    TextView name;
    ImageView sex;
    TextView age;
    TextView order_number;
    TextView speciality;
    RatingBar star;
    ProjectFragment projectFragment;
    WorkerInformationBean bean;

    String user_id;
    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_server_detail);
        titleBarView = (TitleBarView) findViewById(R.id.titleBar);
        titleBarView.setCenterTitle("技师详情");
        titleBarView.setLeftBackButtonIsVisibler(true);
        titleBarView.setListener(TitleBarView.TitleBarEnum.ll_leftBackImageLayout, this);

        titleBarView.setRightBtnTwoIsVisible(true, R.drawable.un_collection_icon);
        titleBarView.setListener(TitleBarView.TitleBarEnum.iv_rightBtnTwo, this);

        Intent intent = this.getIntent();
        bean=(WorkerInformationBean)intent.getSerializableExtra(SERVER_INTENT);

        head=(CircleImageView)findViewById(R.id.head);
        name=(TextView)findViewById(R.id.name);
        order_number=(TextView)findViewById(R.id.order_number);
        speciality=(TextView)findViewById(R.id.speciality);
        star=(RatingBar)findViewById(R.id.star);
        sex=(ImageView)findViewById(R.id.sex);
        age=(TextView)findViewById(R.id.age);

        user_id=MyApplication.local.getString(LocalPreferencesHelper.USER_ID);
        String idstrs=MyApplication.local.getString(user_id+LocalPreferencesHelper.WORKER_IDS);
        String [] ids=idstrs.split("-");
        for(int i = 0;i<ids.length;i++) {
            if (ids[i].equals(bean.id)) {
                titleBarView.setRightBtnTwoIsVisible(true, R.drawable.collection_icon);
                isFavorite = true;
            }
        }


        Glide.with(this)
                .load(HttpProtocolFactory.HOST_URL_USER+bean.head)
                .into(head);
        name.setText(bean.name);
        order_number.setText(bean.order_number + "");
        speciality.setText(bean.speciality);
        star.setRating((float) bean.star);
        age.setText(bean.age+"");
        if(bean.sex.equals("男"))
            sex.setImageResource(R.drawable.sex_b);
        else if(bean.sex.equals("女"))
            sex.setImageResource(R.drawable.sex_g);

        mOrderBt=(Button)findViewById(R.id.order);
        mOrderBt.setOnClickListener(this);
        mNumTv=(TextView)findViewById(R.id.shop_num);
        mNumTv.setText("0");
        mLeftLineIv = (ImageView) findViewById(R.id.left_line);
        mRightLineIv = (ImageView) findViewById(R.id.right_line);

        mShopIv=(ImageView)findViewById(R.id.shop);

        mLeftBt = (Button) findViewById(R.id.left);
        mRightBt = (Button) findViewById(R.id.right);
        mLeftBt.setOnClickListener(this);
        mRightBt.setOnClickListener(this);
        mShopIv.setOnClickListener(this);

        mLeftLineIv.setOnClickListener(this);
        List<Fragment> fragments = new ArrayList<Fragment>();
        projectFragment = new ProjectFragment();
        Bundle arg = new Bundle();
        arg.putString("worker_id",bean.id);
        projectFragment.setArguments(arg);
//        mCallBacks = (Callbacks) projectFragment;
        CommentFragment CommentFragment = new CommentFragment();
        CommentFragment.setArguments(arg);
     //   mCallBacks2 = (Callbacks2) CommentFragment;
        fragments.add(projectFragment);
        fragments.add(CommentFragment);


        FragAdapter adapter = new FragAdapter(getSupportFragmentManager(), fragments);
        vp = (ViewPager) findViewById(R.id.viewpager);
        vp.setOffscreenPageLimit(2);
        vp.setAdapter(adapter);
        //preferences=getSharedPreferences("pager",MODE_PRIVATE);
        // args=preferences.getInt("pager", 0);
        vp.setCurrentItem(0);

        vp.setOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageSelected(int arg0) {

                if (arg0 == 0) {
                    mLeftLineIv.setVisibility(View.VISIBLE);
                    mRightLineIv.setVisibility(View.INVISIBLE);

                } else if (arg0 == 1) {
                    mLeftLineIv.setVisibility(View.INVISIBLE);
                    mRightLineIv.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });

//        initDilog();
    }
   boolean isFavorite=false;
    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.left_back_layout:
                finish();
                break;
            case R.id.left:
                vp.setCurrentItem(0);
                mLeftLineIv.setVisibility(View.VISIBLE);
                mRightLineIv.setVisibility(View.INVISIBLE);
                break;
            case R.id.right:
                vp.setCurrentItem(1);
                mLeftLineIv.setVisibility(View.INVISIBLE);
                mRightLineIv.setVisibility(View.VISIBLE);
                break;
            case R.id.shop:
                showDialog();
                break;
         /*   case R.id.order:
                bean.projects=projectFragment.adapter.getProjects();
                PickerTimeActivity.launch(this,bean);
                break;*/
            case R.id.empty_shop:
                adapter.empty();
                break;
             case R.id.order:
             case R.id.dialog_order:
                 //String user_id=MyApplication.local.getString(LocalPreferencesHelper.USER_ID);
                 if (!user_id.equals(""))
                 {
                     bean.projects=projectFragment.adapter.getProjects();
                     PickerTimeActivity.launch(this,bean);
                 }
                 else
                     show("请登录后操作");
                break;
            case R.id.right_image_button_two:

                String s=MyApplication.local.getString(user_id+LocalPreferencesHelper.WORKER_IDS);
                if(!isFavorite) {
                    titleBarView.setRightBtnTwoIsVisible(true, R.drawable.collection_icon);
                    isFavorite=true;

                    MyApplication.local.saveOrUpdate(user_id+LocalPreferencesHelper.WORKER_IDS,bean.id+"-"+s);
                }
                else {
                    titleBarView.setRightBtnTwoIsVisible(true, R.drawable.un_collection_icon);
                    isFavorite=false;
                    //String s=MyApplication.local.getString(user_id+LocalPreferencesHelper.WORKER_IDS);
                    MyApplication.local.saveOrUpdate(user_id + LocalPreferencesHelper.WORKER_IDS, removeWorkerId(s,bean.id));
                }
                break;


        }

    }


   String removeWorkerId(String ids,String  id)
    {
        String [] strs=ids.split("-");
        String s="";
        for(int i=0;i<strs.length;i++)
        {
            if(strs[i].equals(id))
               strs[i]="-1";
        }
        for(int i=0;i<strs.length;i++)
            if(!strs[i].equals("-1"))
                s=strs[i]+"-";
      /*  if(!s.equals("")) {
            int length = s.length();
            return s.substring(0, length - 1);
        }
        else*/
            return s;
    }
   // View mTotalView;
    TextView mTotalTv;
    Button mEmptyShopBt;
    ShopItemListViewAdapter adapter;
    private Button mDialogOrderBt;
    void showDialog()
    {

        Dialog dialog = new Dialog(this, R.style.Dialog);
        View view= LayoutInflater.from(this).inflate(R.layout.dialog_shop, null);
        dialog.setContentView(view);//R.layout.dialog_shop);
        dialog.setCancelable(true);//只作用返回键
        // dialog.setCanceledOnTouchOutside(true);

        ListView listView =(ListView) dialog.findViewById(R.id.mainListView);
        View headerView= LayoutInflater.from(this).inflate(R.layout.list_shop_header, null);
        // mTotalView=headerView.findViewById(R.id.total_layout);
        mTotalTv=(TextView)headerView.findViewById(R.id.total_num);
        mEmptyShopBt=(Button)headerView.findViewById(R.id.empty_shop);
        mDialogOrderBt=(Button)view.findViewById(R.id.dialog_order);
        mDialogOrderBt.setOnClickListener(this);
        mEmptyShopBt.setOnClickListener(this);
        adapter=new ShopItemListViewAdapter(this,projectFragment.adapter,headerView, view);
        listView.addHeaderView(headerView);
        listView.setAdapter(adapter);
        WindowManager windowManager = getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
 /*       LinearLayout.LayoutParams lp= new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);*/
        lp.width = (int)(display.getWidth()); //设置宽度

        // lp.height=view.getHeight();
   /*     LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.setLayoutParams(lp1);*/

   /*     LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);*/


        Window dialogWindow = dialog.getWindow();
        dialogWindow.setAttributes(lp);
        dialogWindow.setGravity(Gravity.BOTTOM);

        dialog.show();

    }
  /* @Override
    public void change(int number) {
        // TODO Auto-generated method stub
     //  mNumTv.setText("" + number);
    }*/
}









		